package com.sany.struts.action;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.sany.struts.form.Retailer4sCreateForm;
import com.sany.util.DateConverter;
import com.sany.util.SapUtil;
import com.sany.web.beans.Complete4sBean;
import com.sany.web.beans.TireTypeBean;
import com.sany.web.database.DBManager;
import com.sany.web.database.SapDatabase;
import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.IRepository;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Client;
import com.sap.mw.jco.JCO.Function;
import com.sap.mw.jco.JCO.ParameterList;

public class Redetailer4sUpdateAction extends DispatchAction implements
		Serializable {
	private static final long serialVersionUID = 1L;
	public ActionForward updateRedetailer4s(ActionMapping mapping,
			ActionForm forms, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Retailer4sCreateForm retailer4sUpdateForm = (Retailer4sCreateForm) forms;
		DateConverter dateConverter=new DateConverter();
		// sap
		SapDatabase factory = SapUtil.getSapDatabase();
		Client conn = null;
		IRepository iRepository = factory.getConnection();
		IFunctionTemplate ftemplate = iRepository
				.getFunctionTemplate("ZVRI_I_SRP_BP_CHANGE_ALL");
		Function function = new Function(ftemplate);
		conn = JCO.getClient(DBManager.SID);
		ParameterList inparm = function.getImportParameterList();

		//I_LOGON
		JCO.Structure iLogon= inparm.getStructure("I_LOGON");
		com.sany.web.session.WebSession ws=(com.sany.web.session.WebSession) request.getSession().getAttribute("user");
		iLogon.setValue(ws.getSaleOrg(), "VKORG");
		iLogon.setValue(ws.getUserName(), "USERID");
		iLogon.setValue(ws.getBpNo(), "PARTNER");
		iLogon.setValue(ws.getLanguage(), "LANGU");
		//I_SIGN
		inparm.setValue("4S", "I_SIGN");
		//I_HEAD
		inparm.setValue(ws.getBpNo(), "I_HEAD");
		//I_BP_CONTACT_LIST   unuse
		//JCO.Structure contractRcList = inparm.getStructure("I_BP_CONTACT_LIST");
		//I_BP_CUSTOMIZE_HEAD
		JCO.Structure customizeHead = inparm.getStructure("I_BP_CUSTOMIZE_HEAD");
		customizeHead.setValue("","FS_USERID");
		customizeHead.setValue("","FS_CRT_DATE");
		customizeHead.setValue("","FS_VST_DATE");
		customizeHead.setValue("","FS_BRAND");
		customizeHead.setValue("","FS_LSJT");
		customizeHead.setValue(retailer4sUpdateForm.getOpenYear(),"FS_OPENYEAR");
		customizeHead.setValue(retailer4sUpdateForm.getOpenMonth(),"FS_OPENMONTH");
		customizeHead.setValue(retailer4sUpdateForm.getRepairCount(),"FS_CONSULTANTS");
		customizeHead.setValue("","FS_JDGR_NUM");
		customizeHead.setValue("","FS_GW_NUM");
		customizeHead.setValue(retailer4sUpdateForm.getVehicleTrip(),"FS_CLL_NUM");
		customizeHead.setValue("","FS_KXBYBL");
		customizeHead.setValue(retailer4sUpdateForm.getProfit(),"FS_PJPJLRL");
		customizeHead.setValue(retailer4sUpdateForm.getTaskTime(),"FS_GSF");
		customizeHead.setValue("","FS_JZPP1");
		customizeHead.setValue("","FS_JZPP1_DESC");
		customizeHead.setValue("","FS_JZPP1YXL");
		customizeHead.setValue("","FS_JZPP2");
		customizeHead.setValue("","FS_JZPP2_DESC");
		customizeHead.setValue("","FS_JZPP2YXL");
		customizeHead.setValue("","FS_ZXCG");
		customizeHead.setValue("","FS_JTCG");
		customizeHead.setValue("","FS_ZJCCG");
		customizeHead.setValue("","FS_LSDH");
		customizeHead.setValue("","FS_LTCZGSF");
		customizeHead.setValue("","FS_DPHJG");
		customizeHead.setValue(retailer4sUpdateForm.getSetterPrice(),"FS_SLDWJG");
		customizeHead.setValue("","FS_LTYZXL");
		customizeHead.setValue(retailer4sUpdateForm.getJan(),"FS_GYZCXL01");
		customizeHead.setValue(retailer4sUpdateForm.getFeb(),"FS_GYZCXL02");
		customizeHead.setValue(retailer4sUpdateForm.getMar(),"FS_GYZCXL03");
		customizeHead.setValue(retailer4sUpdateForm.getApr(),"FS_GYZCXL04");
		customizeHead.setValue(retailer4sUpdateForm.getMay(),"FS_GYZCXL05");
		customizeHead.setValue(retailer4sUpdateForm.getJun(),"FS_GYZCXL06");
		customizeHead.setValue(retailer4sUpdateForm.getJul(),"FS_GYZCXL07");
		customizeHead.setValue(retailer4sUpdateForm.getAug(),"FS_GYZCXL08");
		customizeHead.setValue(retailer4sUpdateForm.getSep(),"FS_GYZCXL09");
		customizeHead.setValue(retailer4sUpdateForm.getOct(),"FS_GYZCXL10");
		customizeHead.setValue(retailer4sUpdateForm.getNov(),"FS_GYZCXL11");
		customizeHead.setValue(retailer4sUpdateForm.getDec(),"FS_GYZCXL12");
		
		//IT_ITEM06
		JCO.Table itTem06=inparm.getTable("IT_ITEM06");
		List tireTypeList=retailer4sUpdateForm.getTireTypeList();
		for(int i=0;i<tireTypeList.size();i++){
			TireTypeBean tireTypeBean=(TireTypeBean)tireTypeList.get(i);
			itTem06.appendRow();
			itTem06.setValue("","PARTNER");
			//itTem06.setValue("","RECORD_NO");
			itTem06.setValue(tireTypeBean.getFigure(),"FS_LTGGJHW");
			itTem06.setValue(tireTypeBean.getCarType(),"FS_CX");
		}
		
		//IT_ITEM07
		JCO.Table itTem07=inparm.getTable("IT_ITEM07");
		List complete4sList=retailer4sUpdateForm.getComplete4sList(); 
		for(int i=0;i<complete4sList.size();i++){
			Complete4sBean complete4sBean=(Complete4sBean)complete4sList.get(i);
			itTem07.appendRow();
			itTem07.setValue("","PARTNER");
			itTem07.setValue(complete4sBean.getBrand(),"FS_JZPP");
			itTem07.setValue(complete4sBean.getSendType(),"FS_WLMS");
			itTem07.setValue(complete4sBean.getRedetailerPromotion(),"FS_4SDCX");
			itTem07.setValue(complete4sBean.getTerminalPromotion(),"FS_ZDCX");
			itTem07.setValue(complete4sBean.getTrain(),"FS_4SDPX");
			itTem07.setValue(complete4sBean.getStock(),"FS_KC");
		}
		conn.execute(function);
		
		
				return null;
		
	}
}
